<?php

require_once WACT_ROOT . '/db/db.inc.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/site.php';

class LegFilter {
    function doFilter(&$vars){
        $events=array('League','Camp','Clinic','Other');
    	if($vars['published']==1) {
    		if(isset($vars['status']))
    			$status='Published (shown)';
    		else $status='Published (waiting)';
    	} else if($vars['denied']==1)
    		$status='Denied';
    		else $status='Pending';
    	$vars['status']=$status;
    	if(isset($vars['event'])) $vars['event']=$events[$vars['event']];
    	if(isset($vars['premium'])&&$vars['premium']>0)
    		$vars['prem']=1;
        }
}

class League {

    function getPublicURLByID($id) {
    	$url = 'leagues/details.htm?id='.$id;
    	if (APP_NAME!='.') $url = '/'.APP_NAME.'/'.$url;
    	else  $url = '/'.$url;
    	return $url;
    	//return '/'.APP_NAME.'/'.'pub.league.php?league_id='.$id;
    }

    function getIDByName($name) {
     	$rec =& DBC::FindRecord("SELECT league_id
     	from sport_leagues A where A.name = ".DBC::makeLiteral($name)."");
     	if ($rec) return $rec->get('league_id');
     	else return 0;
    }

     function getPublicURLByName($name) {
     		$id = League::getIDByName($name);
     		$url = League::getPublicURLByID($id);
     		return $url;
     }

    function &getUserLeaguesList(&$Pager,$user_id,$option=null) {
        $site_id = Site::getSiteID();
        $isGlobal = Site::IsGlobal();
    	$query="SELECT L.*,C.name as \"category1\",A.name as \"category2\", if(L.date_show<=now(),'true',NULL) as \"status\" ";
    	$query.="  FROM ";
    	$query.=" (sport_leagues L left outer join sport_categories C on L.category_id= C.category_id) ";
    	$query.=" left outer join sport_categories A on category2_id=A.category_id ";
    	$query.=" where user_id=".DBC::makeLiteral($user_id);
    	if($option!=null) $query.=" and L.event=".DBC::makeLiteral($option);
    	$query.=" and (L.site_id = ".DBC::makeLiteral($site_id);
    	if ($isGlobal) $query.=" or 1=1) ";
    	else $query.=") ";
    	error_log("[SQL:] $query");
       	return DBC::NewPagedRecordSet($query,$Pager,new LegFilter());
    }

    function &getEventsList($option=false){
    	if($option)$events=array('(ALL)','League','Camp','Clinic','Other');
    	else $events=array('League','Camp','Clinic','Other');

    	return $events;
    }

    function &getAgeGroups(){
    	$ages = array('Youth','Adult');
    	return $ages;
    }

    function &getAgeGroup($id) {
     	$ages =& League::getAgeGroups();
     	return $ages[$id];
    }


    function getLeaguesCount($user_id) {
        $site_id = Site::getSiteID();
        $isGlobal = Site::IsGlobal();
        $sql = "Select count(*) from sport_leagues
    	where user_id=".DBC::makeLiteral($user_id)."
    	and (site_id = ".DBC::makeLiteral($site_id);
    	if ($isGlobal) $sql.=" or 1=1) ";
    	else $sql.=") ";
    	return DBC::getOneValue($sql);
    }

    function getNewLeaguesCount() {
    	$site_id = Site::getSiteID();
    	return DBC::getOneValue("Select count(*) from sport_leagues where
    	(published = 0 or published is null) and (denied=0 or denied is null)
        and site_id = ".DBC::makeLiteral($site_id));
    }

	function delete($id) {
        DBC::execute("DELETE FROM sport_leagues WHERE league_id=". DBC::makeLiteral($id));
    }

    function &getEventName($id) {
     	$events =& League::getEventsList();
     	return $events[$id];
    }

    function insertLeague(&$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        return $Record->insertId('sport_leagues',
            array('user_id', 'category_id','category2_id', 'date_added', 'event', 'name',
            'org_title', 'org_phone', 'org_email', 'org_address', 'org_zip', 'org_city', 'org_state',
            'premium', 'published', 'additional', 'age','site_id'),
            'league_id');
    }

    function &getRecord($id) {
        return DBC::FindRecord("SELECT U.*, L.*, C1.name as category1, C2.name as category2
        FROM sport_leagues L
        left outer join sport_users U on L.user_id = U.user_id
        left outer join sport_categories C1 on L.category_id = C1.category_id
     	left outer join sport_categories C2 on L.category2_id = C2.category_id
        WHERE L.league_id = ". DBC::makeLiteral($id));
    }

    function updateLeague($id, &$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        $Record->update('sport_leagues',
            array('user_id', 'category_id','category2_id', 'event', 'name',
            'org_title', 'org_phone', 'org_email', 'org_address', 'org_zip', 'org_city', 'org_state',
            'premium', 'published', 'additional', 'age','site_id'),
            "league_id=". DBC::makeLiteral($id));
    }
    function denyLeague($id) {
        $data = new DataSpace();
        $data->set('denied',1);
        $data->set('published',0);
    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_leagues',
            array('denied', 'published'),
            "league_id=". DBC::makeLiteral($id));
    }

    function publishLeague($id) {
        $data = new DataSpace();
        $data->set('denied',0);
        $data->set('published',1);
    	$Record =& DBC::NewRecord($data);
        $Record->update('sport_leagues',
            array('denied', 'published'),
            "league_id=". DBC::makeLiteral($id));
    }

    // get the next new league
    function &getNextNewLeague($current_league_id) {
    	$site_id = Site::getSiteID();
        $sql = "select A.* from sport_leagues A, sport_leagues A2
        where A.league_id <> A2.league_id
        and A2.league_id = ".DBC::makeLiteral($current_league_id)."
        and A.date_added <= A2.date_added
        and (A.published = 0  or  A.published is null)
        and A.site_id = ".DBC::makeLiteral($site_id)."
        and A2.site_id = ".DBC::makeLiteral($site_id)."
        order by A.date_added desc limit 1";
        return DBC::FindRecord($sql);
    }

    function &getLeaguesList(&$Pager, &$request, &$responseModel,$public=false) {
        $site_id = Site::getSiteID();
        $isGlobal = Site::IsGlobal();
    	//restore filter from session
     	Session::loadResponseModel($responseModel,'filter.leagues');
     	$sql = "select L.*, U.*, C1.name as category1, C2.name as category2
     	from sport_leagues L
     	left outer join sport_users U on L.user_id = U.user_id
     	left outer join sport_categories C1 on L.category_id = C1.category_id
     	left outer join sport_categories C2 on L.category2_id = C2.category_id
     	where 1=1 and L.published=1 ".
        " and (L.site_id = ".DBC::makeLiteral($site_id)." ";
        if ($isGlobal) $sql.=" or 1=1) ";
    	else $sql.=") ";
     	$sql .= League::getLeaguesFilter($request, $responseModel);
     	if($public) $sql.=" and L.date_added<=now() ";
     	$sql.=" order by ".League::getLeaguesOrder($request, $responseModel);
     	if($public) return DBC::NewPagedRecordSet($sql, $Pager, new LegFilter());
        return DBC::NewPagedRecordSet($sql, $Pager);
     }

     function &getNewLeaguesList(&$Pager, &$request, &$responseModel) {
     	$site_id = Site::getSiteID();
        $sql = "select L.*, U.*, C1.name as category1, C2.name as category2
     	from sport_leagues L
     	left outer join sport_users U on L.user_id = U.user_id
     	left outer join sport_categories C1 on L.category_id = C1.category_id
     	left outer join sport_categories C2 on L.category2_id = C2.category_id
        where 1=1 and (L.published=0 or L.published is null)
        and L.site_id = ".DBC::makeLiteral($site_id)."
     	order by L.date_added desc ";
     	return DBC::NewPagedRecordSet($sql, $Pager);
      }

     function getLeaguesFilter(&$request, &$responseModel) {
      	$where = '';

      	$filter_event_id = $responseModel->get('filter_event_id');
        $event_id = $responseModel->get('event_id');
      	if ($filter_event_id)  $where .= ' and L.event = '.DBC::makeLiteral($event_id);

        $filter_category1 = $responseModel->get('filter_category1');
        $category1 = $responseModel->get('category1');
        if ($filter_category1)  $where .= ' and L.category_id = '.DBC::makeLiteral($category1);

        $filter_category2 = $responseModel->get('filter_category2');
        $category2 = $responseModel->get('category2');
        if ($filter_category2)  $where .= ' and L.category2_id = '.DBC::makeLiteral($category2);

        return $where;
      }

      function getLeaguesOrder(&$request, &$responseModel) {
      	$sortby = 'L.date_added';
      	$sortby = $responseModel->get('sort');
      	if ($sortby) return 'L.'.$sortby;
      	else return 'L.date_added';
      }


}

?>
